package com.example.a22310.myapplication.Model;

import com.example.a22310.myapplication.Beans.NewBean;
import com.example.a22310.myapplication.Beans.Train;
import com.example.a22310.myapplication.Listener.TrainListener;
import com.example.a22310.myapplication.Service.service;
import com.example.a22310.myapplication.ViewModel.Train.AddNewsViewModel;
import com.example.a22310.myapplication.ViewModel.Train.ConsultViewModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.provider.ContactsContract.CommonDataKinds.Website.URL;

public class TrainModel {
    private Retrofit retrofit;
    public TrainModel()
    {
        retrofit= new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void trainList(final TrainListener<Train> listener, int flag) {  //培训列表
        service userService = retrofit.create(service.class);
        Call<List<Train>> call = userService.trainlist(flag);
        Callback<List<Train>> callback = new Callback<List<Train>>() {
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
                List<Train> train = response.body();
                listener.onSuccess(train);
            }
            @Override
            public void onFailure(Call<List<Train>> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    public void newsList(int club_id, final ConsultViewModel listener) {  //培训之消息列表
        service userService = retrofit.create(service.class);
        Call<List<NewBean>> call = userService.newslist(club_id);
        Callback<List<NewBean>> callback = new Callback<List<NewBean>>() {
            @Override
            public void onResponse(Call<List<NewBean>> call, Response<List<NewBean>> response) {
                List<NewBean> newses = response.body();
                listener.onSuccess(newses);
            }
            @Override
            public void onFailure(Call<List<NewBean>> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    public void addNews(NewBean newBean, final AddNewsViewModel listener) {  //登录
        service userService = retrofit.create(service.class);
        Call<NewBean> call = userService.addnews(newBean.getClub_id(),newBean.getSend_id(),newBean.getContent());
        Callback<NewBean> callback = new Callback<NewBean>() {
            @Override
            public void onResponse(Call<NewBean> call, Response<NewBean> response) {
                NewBean newBean1 = response.body();
                listener.onSuccess(newBean1);
            }
            @Override
            public void onFailure(Call<NewBean> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }
}
